//
//  MyKeepShoppingViewController.m
//  lmq
//
//  Created by apple on 2017/3/9.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "MyKeepShoppingViewController.h"
#import "MyKeepAllTableViewCell.h"
#import "MyCollectionShoppingModel.h"
#import "UIImageView+WebCache.h"
#import "detailsViewController.h"


@interface MyKeepShoppingViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation MyKeepShoppingViewController
//正在热卖
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationDate];
    [self creationView];
    [self regeditCell];
    
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    self.navigationItem.title = @"我的收藏";
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64);
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}
- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MyKeepAllTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyKeepAllTableViewCell"];
    
}
- (void)zj_viewDidLoadForIndex:(NSInteger)index {
    //    NSLog(@"%@",self.view);
    //    NSLog(@"%@", self.zj_scrollViewController);
    
    
}
- (void)zj_viewWillAppearForIndex:(NSInteger)index {
    // NSLog(@"viewWillAppear------");
    
    
    
}


- (void)zj_viewDidAppearForIndex:(NSInteger)index {
    // NSLog(@"viewDidAppear-----");
    
}

#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 110;
    
}
#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyKeepAllTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyKeepAllTableViewCell" forIndexPath:indexPath];
    
    MyCollectionShoppingModel *model = self.dataArr[indexPath.row];
    cell.modelShopping = model;
   
    
    return cell;
    
    
}
- (void)creationDate{

    _dataArr = [[NSMutableArray alloc]init];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    NSString *memberId = [PublicFunction getConfigWithKey:@"uid"];
    [productParams setValue:tokenStr forKey:@"token"];
    [productParams setValue:@"1" forKey:@"type"];
    [productParams setValue:memberId forKey:@"member_id"];
    
    [[MNetworkHandler sharedInstance]conURL:My_AccordingToCollection
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       NSDictionary *dict1=[returnData objectForKey:@"data"];
                                       ;


                                       
                                       for (NSDictionary *dic in dict1 ) {
                                           MyCollectionShoppingModel *model = [[MyCollectionShoppingModel alloc]init];
                                           [model setValuesForKeysWithDictionary:dic];
                                           [_dataArr addObject:model];
                                       }

                                       
                                       [self.tableView reloadData];
                                   }
                                   else if ([[returnData objectForKey:@"code"]intValue]==-1){
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];

}
#pragma mark - ---------- cell可以编辑 ----------
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark - ---------- cell编辑样式 ----------
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

#pragma mark - ---------- 进入编辑模式，按下出现的编辑按钮后,进行删除操作 ----------
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {

        NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
        NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
        NSString *memberId = [PublicFunction getConfigWithKey:@"uid"];
        [productParams setValue:tokenStr forKey:@"token"];
        [productParams setValue:memberId forKey:@"member_id"];
        MyCollectionShoppingModel *model = self.dataArr[indexPath.row];
        productParams[@"id"] = model.id;
        
        [[MNetworkHandler sharedInstance]conURL:My_RemoveAccordingToCollection
                                    networkType:NetWorkPOST
                                         params:productParams
                                       delegate:nil
                                        showHUD:NO
                                   successBlock:^(id returnData) {
                                       if ([[returnData objectForKey:@"code"]intValue]==1) {
                                           
                                           
                                           [self.tableView reloadData];
                                       }
                                       else {
                                           [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       }
                                   } failureBlock:^(NSError *error) {
                                       NSLog(@"%@",error);
                                       [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                                   }];

        
        
        [self.dataArr removeObjectAtIndex:indexPath.row];  //删除数组里的数据
        [self.tableView  deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];  //删除对应数据的cell
        
    }
    
    
}
#pragma mark - ---------- 删除 ----------
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"取消收藏";
}
#pragma mark - ---------- 每个Cell的点击事件 ----------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    detailsViewController *detailsVC=[[detailsViewController alloc]init];
    detailsVC.hidesBottomBarWhenPushed=YES;
    
    MyCollectionShoppingModel *model = self.dataArr[indexPath.row];
    detailsVC.myStr = model.shop_id;
    
    
    [self.navigationController pushViewController:detailsVC animated:YES];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
